package com.lphj.datainterface.handle;

import com.lphj.datainterface.component.Config.ReadGoodsConfig;
import com.lphj.datainterface.component.Config.ReadMCGoodsConfig;
import com.lphj.datainterface.entity.TCommon;
import com.lphj.datainterface.entity.TGoods;
import com.lphj.datainterface.service.TCommonService;
import com.lphj.datainterface.service.TGoodsService;
import com.lphj.datainterface.service.TGoods_FreeNOService;
import com.lphj.datainterface.util.ToPy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Component
public class TGoodsForMCHandle {

    @Autowired
    TGoodsService tGoodsService;

    @Autowired
    TCommonService tCommonService;

    @Autowired
    ReadGoodsConfig readGoodsConfig;

    @Autowired
    ReadMCGoodsConfig readMCGoodsConfig;

    @Autowired
    TGoods_FreeNOService tGoods_freeNOService;

    @Autowired
    TStoreGoodsHandle tStoreGoodsHandle;

    @Autowired
    TStoreGoodsVendorHandle tStoreGoodsVendorHandle;

    @Transactional
    public int addTGoods(Sheet sheet) {
        int count = 0;
        boolean isNotNull = true;
        int index = 0;

        //获取第一行标题
        Row firstRow = sheet.getRow(0);
        //获取首行标题
        List<String> firstRowTitle = new ArrayList<String>();
        if(sheet != null) {
            //清空商品表tGoods
            tGoodsService.delTGoodsByAll();
            //获取首行标题
            while (isNotNull) {
                Cell cell = firstRow.getCell(index);
                if (cell != null) {
                    firstRowTitle.add(cell.getStringCellValue());
                } else {
                    isNotNull = false;
                }
                index++;
            }
            int rg_goodsId = 0;
            int rg_goodsName = 0;
            int rg_goodsDesc = 0;
            int rg_goodsSpec = 0;
            int rg_goodsUnit = 0;
            int rg_salePrice = 0;
            int rg_supPrice = 0;

            for (int j = 0; j < firstRowTitle.size(); j++) {
                if (firstRowTitle.get(j).equals(readGoodsConfig.getGoodsId())) {
                    rg_goodsId = j;
                } else if (firstRowTitle.get(j).equals(readGoodsConfig.getGoodsName())) {
                    rg_goodsName = j;
                } else if (firstRowTitle.get(j).equals(readGoodsConfig.getGoodsSpec())) {
                    rg_goodsSpec = j;
                } else if (firstRowTitle.get(j).equals(readGoodsConfig.getGoodsUnit())) {
                    rg_goodsUnit = j;
                } else if (firstRowTitle.get(j).equals(readGoodsConfig.getSalePrice())) {
                    rg_salePrice = j;
                } else if (firstRowTitle.get(j).equals(readGoodsConfig.getSupPrice())) {
                    rg_supPrice = j;
                } else if (firstRowTitle.get(j).equals(readGoodsConfig.getGoodsDesc())) {
                    rg_goodsDesc = j;
                }
            }
            //创建单元格
            Cell cell;
            //设置税率
            TCommon saleCommon = tCommonService.getCommonByCommonNOAndComID(readGoodsConfig.getsTax(), readGoodsConfig.getSaleTax());
            TCommon buyCommon = tCommonService.getCommonByCommonNOAndComID(readGoodsConfig.getbTax(), readGoodsConfig.getBuyTax());
            for (int r = 1; r <= sheet.getLastRowNum(); r++) {
                //创建数据对象
                TGoods tGoods = new TGoods();
                //读取第r行
                Row row = sheet.getRow(r);
                //获取ERP商品id
                tGoods.setGoodsID(tGoodsService.getMaxId() + 1);
                //获取商品ERP编号
                tGoods.setGoodsNO(tGoods_freeNOService.getTGoods_FreeNOByTopOne().getsGoodsNO());
                tGoods_freeNOService.remTGoods_RreeNO(tGoods.getGoodsNO());
                if (row == null) {
                    continue;
                }
                //获取外部系统商品ID
                cell = row.getCell(rg_goodsId);
                if (cell != null) {
                    //cell.setCellType(CELL_TYPE_STRING);
                    //customerType.setGm_customerNO(cell.getStringCellValue());
                    tGoods.setGoodsNO2(cell.getStringCellValue());
                }
                //获取商品简称
                cell = row.getCell(rg_goodsName);
                if (cell != null) {
                    tGoods.setGoodsName(cell.getStringCellValue());
                }
                cell = row.getCell(rg_goodsDesc);
                if (cell != null) {
                    tGoods.setGoodsDesc(cell.getStringCellValue());
                }
                //获取商品规格
                cell = row.getCell(rg_goodsSpec);
                if (cell != null) {
                    tGoods.setSpec(cell.getStringCellValue());
                }
                //获取商品单位
                cell = row.getCell(rg_goodsUnit);
                if (cell != null) {
                    tGoods.setUnit(cell.getStringCellValue());
                }
                //获取商品销售价
                cell = row.getCell(rg_salePrice);
                if (cell != null) {
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    tGoods.setSalePrice(Double.parseDouble(cell.getStringCellValue()));
                    tGoods.setVipPrice(Double.parseDouble(cell.getStringCellValue()));
                }
                //获取商品采购价
                cell = row.getCell(rg_supPrice);
                if (cell != null) {
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    tGoods.setBuyPrice(Double.parseDouble(cell.getStringCellValue()));
                }
                tGoods.setGUID(UUID.randomUUID().toString());
                //设置商品拼音
                tGoods.setPYCode(ToPy.ToFirstChar(tGoods.getGoodsName()));
                //获取销售税率
                tGoods.setSaleTax(saleCommon.getComDesc());
                tGoods.setSaleTaxID(saleCommon.getComID());
                tGoods.setSaleTaxPct(saleCommon.getNum1());
                //获取进货税率
                tGoods.setBuyTax(buyCommon.getComDesc());
                tGoods.setBuyTaxID(buyCommon.getComID());
                tGoods.setBuyTaxPct(buyCommon.getNum1());
                tGoods.setCategoryID(119);
                //获取条码
                Map<String, Object> paramterMap = new HashMap<String, Object>();
                paramterMap.put("Result", "");
                paramterMap.put("GoodsNO", tGoods.getGoodsNO());
                tGoodsService.getBarCode(paramterMap);
                tGoods.setMainBarcode(paramterMap.get("Result").toString());
                //所属部门
                tGoods.setDeptID(1);
                tGoods.setGoodsTypeID("1");
                tGoods.setGoodsType("标准商品");
                tGoods.setTradeModeID("1");
                tGoods.setTradeMode("购销");
                tGoods.setBrandID("");
                tGoods.setHomeID("");
                tGoods.setHome("中国");
                tGoods.setGoodsLevelID("1");
                tGoods.setGoodsLevel("正品");
                tGoods.setMinOrderQty(1.00);
                tGoods.setMainVendorID(1);
                tGoods.setCaseUnits(1);
                tGoods.setMakeQty(1.00);
                tGoods.setStatus(0);
                tGoods.setTag(222);
                tGoods.setPosTypeID("1");
                tGoods.setPosType("普通码");
                tGoods.setCreateUser("管理员");
                tGoods.setChangeUser("管理员");
                tGoods.setTaxGoodsType("免税商品");
                tGoods.setTaxGoodsTypeID("1");
                tGoods.setLossRate(0.00);
                tGoods.setLastUpdateTime(new Date());

                //添加商品
                count += tGoodsService.addTGoods(tGoods);
                tStoreGoodsHandle.addTStoreGoods(tGoods);
                tStoreGoodsVendorHandle.addTStoreGoodsVendor(tGoods);
            }
        }
        return count;
    }

    /**
     * 统一观麦和买菜系统的商品信息 美菜系统的商品ID写入到TGoods表里的sGoodsNO3字段
     * @param sheet
     * @return
     */
    @Transactional
    public Map<String,Object> unifiedTGoods(Sheet sheet) {
        int count = 0;
        boolean isNotNull = true;
        int index = 0;
        Map<String,Object> data = new HashMap<>();

        //获取第一行标题
        Row firstRow = sheet.getRow(0);
        //获取首行标题
        List<String> firstRowTitle = new ArrayList<String>();
        if(sheet != null) {
            //获取首行标题
            while (isNotNull) {
                Cell cell = firstRow.getCell(index);
                if (cell != null) {
                    firstRowTitle.add(cell.getStringCellValue());
                } else {
                    isNotNull = false;
                }
                index++;
            }
            int mc_goodsId = 0;
            int mc_gmGoodsId = 0;
            int mc_gmGoodsName = 0;

            for (int j = 0; j < firstRowTitle.size(); j++) {
                if (firstRowTitle.get(j).equals(readMCGoodsConfig.getGoodsId())) {
                    mc_goodsId = j;
                } else if (firstRowTitle.get(j).equals(readMCGoodsConfig.getGmGoodsId())) {
                    mc_gmGoodsId = j;
                } else if (firstRowTitle.get(j).equals(readMCGoodsConfig.getGmGoodsName())) {
                    mc_gmGoodsName = j;
                }
            }
            //读取单元格
            Cell cell;
            for (int r = 1; r <= sheet.getLastRowNum(); r++) {
                //读取第r行
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                String goodsId = "";
                String gmGoodsId = "";
                String gmGoodsName = "";
                //获取观麦商品ID
                cell = row.getCell(mc_gmGoodsId);
                if (cell != null) {
                    gmGoodsId = cell.getStringCellValue();
                }

                //获取观麦商品名称
                cell = row.getCell(mc_gmGoodsName);
                if (cell != null) {
                    gmGoodsName = cell.getStringCellValue();
                }

                //获取美菜商品ID
                cell = row.getCell(mc_goodsId);
                if (cell != null) {
                    cell.setCellType(CellType.STRING);
                    goodsId = cell.getStringCellValue();
                }

                //创建数据对象
                if(tGoodsService.unifiedTGoods(gmGoodsId,gmGoodsName,goodsId) > 0) {
                    count ++;
                } else {
                    data.put("error:"+goodsId,goodsId);
                }
            }
        }
        data.put("success",count);
        return data;
    }
}
